/**
 * 
 */
package org.lightcommons.tasks;

import java.util.List;

/**
 * @author GL
 * 
 * @since 2008-1-9
 */
public class ChainExecutor {
	private List<ChainTask> interceptors;
	
	/**
	 * @param interceptors the interceptors to set
	 */
	public void setInterceptors(List<ChainTask> interceptors) {
		this.interceptors = interceptors;
	}
	
	public void launch(){
		invoke(0);
	}

	/**
	 * @param ii
	 */
	private void invoke(int x) {
		int i=x;
		ChainTask in = interceptors.get(i);
		try {
			if(in.chain() && i<interceptors.size()-1){
				invoke(++i);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		in.after();
	}
}
